package com.hengpeng.itfinoss.service.userTypeApply.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.hengpeng.common.exception.ResourceNotFoundException;
import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.persist.QueryResult;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.Restrictions;
import com.hengpeng.common.persist.query.constants.QueryConstants.QueryType;
import com.hengpeng.itfinbase.en.UserAppStatus;
import com.hengpeng.itfinbase.persist.UserInfo;
import com.hengpeng.itfinbase.persist.UserTypeApply;
import com.hengpeng.itfinoss.service.ServiceFactory;
import com.hengpeng.itfinoss.service.userTypeApply.UserTypeApplyService;

@Service
@Lazy
public class UserTYpeApplyServiceImpl implements UserTypeApplyService {
    @Autowired
    private ServiceFactory serviceFactory;
	@Override
	public ServiceResult<List<?>> selectUserTypeApplyPage(int indexPage,
			int pageSize) {
		DynamicQuery query = new DynamicQuery(UserTypeApply.class,"type");
		query.createAlias(UserInfo.class,"user");
		query.add(Restrictions.eqProperty("type.userId", "user.id"));
		query.desc("type.createTime");
		query.setQueryType(QueryType.JDBC);
		query.setIsSearchTotalCount(true);
		query.setPageIndex(indexPage);
		query.setPageSize(pageSize);
		System.out.println(query.getSqlResult().getSql());
		QueryResult requery = serviceFactory.getPersistService().findWithPage(null, query);
        List<?> list = requery.getResults();
        Long count = requery.getTotalCount();
        ServiceResult<List<?>> serviceResult = new ServiceResult<List<?>>();//设置返回值
        serviceResult.setReturnValue(list);
        serviceResult.setReturnListSize(count.intValue());
        return serviceResult;
	}
    @Override
    public UserTypeApply modify(String userApplyStatus, Long id) {
        UserTypeApply userapp=serviceFactory.getPersistService().get(UserTypeApply.class, id);
           
        if (userapp == null)
        {
            throw new ResourceNotFoundException(id);
        }
        Date date=new Date();
        userapp.setUpdateTime(date);
        if(userApplyStatus!=null){
            userapp.setUserApplyStatus(UserAppStatus.valueOf(userApplyStatus));
        }
        return serviceFactory.getPersistService().update(userapp);
    }
}
